<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="领料单号" prop="serialNo">
        <el-input
          v-model="queryParams.serialNo"
          placeholder="请输入领料单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取日期" prop="requisitionDate">
        <el-date-picker clearable
          v-model="queryParams.requisitionDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择领取日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请人" prop="applyUser">
        <el-input
          v-model="queryParams.applyUser"
          placeholder="请输入申请人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker clearable
          v-model="queryParams.applyTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核人" prop="auditUser">
        <el-input
          v-model="queryParams.auditUser"
          placeholder="请输入审核人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker clearable
          v-model="queryParams.auditTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核意见" prop="auditComment">
        <el-input
          v-model="queryParams.auditComment"
          placeholder="请输入审核意见"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:requisition:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:requisition:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:requisition:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:requisition:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="requisitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="领料单号" align="center" prop="serialNo" />
      <el-table-column label="生产计划" align="center" prop="mesProductSchedules[0].serialNo" />
      <el-table-column label="计划排产" align="center" prop="mesProductPlans[0].serialNo" />
      <el-table-column label="产品" align="center" prop="wmsMaterialInfoList[0].name" />
      <el-table-column label="型号" align="center" prop="wmsMaterialInfoList[0].model" />
      <el-table-column label="规格" align="center" prop="wmsMaterialInfoList[0].specification" />
      <el-table-column label="单位" align="center" prop="wmsMaterialInfoList[0].unit" />
      <el-table-column label="领取日期" align="center" prop="requisitionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.requisitionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="nickName" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="applyStatus" />
      <el-table-column label="审核人" align="center" prop="nickName" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:requisition:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:requisition:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改领料单对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="requisitionRef" :model="form" :rules="rules" label-width="100px">
        <div style="display: flex"> 
          <el-form-item label="领料单号" prop="serialNo">
          <el-input v-model="form.serialNo" placeholder="请输入领料单号" />
        </el-form-item>
        <el-form-item label="计划排产" prop="scheduleId">
          <el-input v-model="form.scheduleId" placeholder="请输入计划排产" />
        </el-form-item>
        <el-form-item label="产品" prop="name">
          <el-input v-model="form.name"  />
        </el-form-item></div>
       <div style="display: flex">
         <el-form-item label="型号" prop="productId">
          <el-input v-model="form.productId"  />
        </el-form-item>
        <el-form-item label="规格" prop="productId">
          <el-input v-model="form.productId"  />
        </el-form-item>
        <el-form-item label="单位" prop="productId">
          <el-input v-model="form.productId"  />
        </el-form-item>
      </div>
        <div style="display: flex">
          <el-form-item label="待产数量" prop="productId">
          <el-input v-model="form.productId"  />
        </el-form-item>
           <el-form-item label="生产日期" prop="logDate">
          <el-date-picker clearable
            v-model="form.logDate"
            type="date"
            value-format="YYYY-MM-DD">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取日期" prop="requisitionDate">
          <el-date-picker clearable
            v-model="form.requisitionDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择领取日期">
          </el-date-picker>
        </el-form-item>
      </div>
      <div style="display: flex">
        <el-form-item label="申请人" prop="applyUser">
          <el-input v-model="form.applyUser"/>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="date"
            value-format="YYYY-MM-DD">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="auditUser">
          <el-input v-model="form.auditUser"/>
        </el-form-item>
      </div>
        <div style="display: flex">
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
            v-model="form.auditTime"
            type="date"
            value-format="YYYY-MM-DD">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditUser">
          <el-input v-model="form.auditUser"/>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditUser">
          <el-input v-model="form.auditUser"/>
        </el-form-item></div>
        
        <el-divider content-position="center">领料单物料明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddMesMaterialRequisitionDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteMesMaterialRequisitionDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="mesMaterialRequisitionDetailList" :row-class-name="rowMesMaterialRequisitionDetailIndex" @selection-change="handleMesMaterialRequisitionDetailSelectionChange" ref="mesMaterialRequisitionDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column property="id" label="编号" width="80" />
                <el-table-column property="name" label="物料" width="80" />
                <el-table-column property="model" label="型号" width="80" />
                <el-table-column property="specification" label="规格" width="100" />
                <el-table-column property="unit" label="单位" width="80" />
                <el-table-column property="price" label="需求数量" width="80" />
                <el-table-column property="type" label="已领数量" width="80" />
                <el-table-column property="remark" label="备注" width="50" />
                <el-table-column label="操作" width="80">
                    <template v-slot="scope">
                        <el-button @click="xzwl(scope.row,wxz), xz = false;" style="display: inline; width: 50px;">
                            选择
                        </el-button>
                    </template>
                </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Requisition">
import { listRequisition, getRequisition, delRequisition, addRequisition, updateRequisition } from "@/api/system/requisition";

const { proxy } = getCurrentInstance();

const requisitionList = ref([]);
const mesMaterialRequisitionDetailList = ref([]);
const open = ref(false);
const xz=ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedMesMaterialRequisitionDetail = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const wldata=ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serialNo: null,
    requisitionDate: null,
    applyUser: null,
    applyTime: null,
    applyStatus: null,
    auditUser: null,
    auditTime: null,
    auditStatus: null,
    auditComment: null,
  },
  rules: {
    serialNo: [
      { required: true, message: "计划排产不能为空", trigger: "blur" }
    ],
    requisitionDate: [
      { required: true, message: "领取日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询领料单列表 */
function getList() {
  loading.value = true;
  listRequisition(queryParams.value).then(response => {
    requisitionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    serialNo: null,
    planId: null,
    scheduleId: null,
    productId: null,
    requisitionDate: null,
    applyUser: null,
    applyTime: null,
    applyStatus: null,
    auditUser: null,
    auditTime: null,
    auditStatus: null,
    auditComment: null,
    status: null,
    remark: null
  };
  mesMaterialRequisitionDetailList.value = [];
  proxy.resetForm("requisitionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加领料单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRequisition(_id).then(response => {
    form.value = response.data;
    mesMaterialRequisitionDetailList.value = response.data.mesMaterialRequisitionDetailList;
    open.value = true;
    title.value = "修改领料单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["requisitionRef"].validate(valid => {
    if (valid) {
      form.value.mesMaterialRequisitionDetailList = mesMaterialRequisitionDetailList.value;
      if (form.value.id != null) {
        updateRequisition(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRequisition(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除领料单编号为"' + _ids + '"的数据项？').then(function() {
    return delRequisition(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 领料单物料明细序号 */
function rowMesMaterialRequisitionDetailIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 领料单物料明细添加按钮操作 */
function handleAddMesMaterialRequisitionDetail() {
  let obj = {};
  obj.materialId = "";
  obj.requireQuantity = "";
  obj.receiveQuantity = "";
  obj.detailRemark = "";
  mesMaterialRequisitionDetailList.value.push(obj);
}

/** 领料单物料明细删除按钮操作 */
function handleDeleteMesMaterialRequisitionDetail() {
  if (checkedMesMaterialRequisitionDetail.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的领料单物料明细数据");
  } else {
    const mesMaterialRequisitionDetails = mesMaterialRequisitionDetailList.value;
    const checkedMesMaterialRequisitionDetails = checkedMesMaterialRequisitionDetail.value;
    mesMaterialRequisitionDetailList.value = mesMaterialRequisitionDetails.filter(function(item) {
      return checkedMesMaterialRequisitionDetails.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleMesMaterialRequisitionDetailSelectionChange(selection) {
  checkedMesMaterialRequisitionDetail.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/requisition/export', {
    ...queryParams.value
  }, `requisition_${new Date().getTime()}.xlsx`)
}

getList();
</script>
